package stu.kurs.services.impl;

import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import stu.kurs.domain.Match;
import stu.kurs.exceptions.ServiceException;
import stu.kurs.services.ICategoryService;
import stu.kurs.services.IMatchService;
import stu.kurs.services.impl.GenericService;

@Stateless(name="MatchService")
public class MatchService extends GenericService<Match> implements IMatchService {
	
	@EJB(beanName="CategoryService")
	private ICategoryService CategoryService;
	
	public MatchService(){
		super(Match.class);
	}

	public void add(String team1, String team2, Date date, Integer scoreteam1, Integer scoreteam2, 
			        Double koefteam1, Double koefteam2,	Integer idcategory) throws ServiceException{
		 Match match = new Match(team1, team2, scoreteam1, scoreteam2, koefteam1, koefteam2, new Date());
		 match.setCategory(CategoryService.findById(idcategory));
		 save(match);
	}

	@Override
	public void update(Integer id, String team1, String team2, Integer scoreteam1, Integer scoreteam2,
			Double koefteam1, Double koefteam2, Date date)	throws ServiceException {
		Match match = findById(id);
		match.setTeam1(team1);
		match.setTeam2(team2);
		match.setScoreteam1(scoreteam1);
		match.setScoreteam2(scoreteam2);
		match.setKoefteam1(koefteam1);
		match.setKoefteam2(koefteam2);
		match.setDate(date);
		save(match);
		
	}

	@Override
	public void delete(Integer id) throws ServiceException {
		Match match = findById(id);
		delete(match);
		
	}

	@Override
	public List<Match> findByDates(Date start, Date end, Integer idcategory) throws ServiceException {
		List<Match> result = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Match> cq =cb.createQuery(Match.class);
		Root<Match> match = cq.from(Match.class);
		cq.where(cb.and(
				(cb.greaterThanOrEqualTo(match.<Date>get("date"), start)),
				(cb.lessThanOrEqualTo(match.<Date>get("date"), end)),
				(cb.equal(match.get("category").get("id"), idcategory))
				));
		TypedQuery<Match> q = em.createQuery(cq); 
		try{
			result = q.getResultList();
		}
		catch (Exception e){
			throw new ServiceException(e);
		} 
		return result;
	}
      
	@Override
	public List<Match> getMatchByDates(Date start, Date end) throws ServiceException {
		List<Match> result = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Match> cq =cb.createQuery(Match.class);
		Root<Match> match = cq.from(Match.class);
		cq.where(cb.and((cb.greaterThanOrEqualTo(match.<Date>get("date"), start)),
				(cb.lessThanOrEqualTo(match.<Date>get("date"), end))));
        cq.orderBy(cb.asc(match.get("date")));
		TypedQuery<Match> q = em.createQuery(cq); 
		try{
			result = q.getResultList();
		}
		catch (Exception e){
			throw new ServiceException(e);
		} 
		return result;
	}

	@Override
	public void changeScore(Integer scoreteam1, Integer scoreteam2, Integer id)	throws ServiceException {
		Match match = findById(id);
		match.setScoreteam1(scoreteam1);
		match.setScoreteam2(scoreteam2);
		save(match);	
	}

	@Override
	public void changeKoef(Double koefteam1, Double koefteam2, Integer id) 	throws ServiceException {
		Match match = findById(id);
		match.setKoefteam1(koefteam1);
		match.setKoefteam2(koefteam2);
		save(match);
	}

	@Override
	public void changeDate(Date date, Integer id) throws ServiceException {
		Match match = findById(id);
		match.setDate(date);
		save(match);
		
	}

	@Override
	public List<Match> getAllByCategoryId(Integer idcategory)	throws ServiceException {
		List<Match> result = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Match> cq = cb.createQuery(Match.class);
		Root<Match> match = cq.from(Match.class);
		cq.where(cb.equal(match.get("category").get("id"), idcategory));
		cq.orderBy(cb.desc(match.get("date")));
		TypedQuery<Match> q = em.createQuery(cq); 
		try{
			result = q.getResultList();
		}
		catch (Exception e){
			throw new ServiceException(e);
		} 
		return result;
	}	

	@Override
	public List<Match> getAllMatch()	throws ServiceException {
		List<Match> result = null;
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Match> cq = cb.createQuery(Match.class);
		Root<Match> match = cq.from(Match.class);
		cq.orderBy(cb.asc(match.get("date")));
		TypedQuery<Match> q = em.createQuery(cq); 
		try{
			result = q.getResultList();
		}
		catch (Exception e){
			throw new ServiceException(e);
		} 
		return result;
	}
	
	
}
